package com.haoshuang.zhouzhoubang.ui.activity

import android.content.Context
import android.os.Bundle
import com.haoshuang.zhouzhoubang.databinding.ActivityExtraCostsPayBinding
import com.haoshuang.zhouzhoubang.ui.vm.ExtraCostsPayViewModel
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.ktx.getString
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.load


/**
 * 额外费用收款
 */
class ExtraCostsPayActivity :
    BaseVMActivity<ExtraCostsPayViewModel, ActivityExtraCostsPayBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<ExtraCostsPayActivity>()
        }
    }

    // 订单id
    private var orderId = ""
    // 费用名称
    private var payName = ""

    override fun initView(savedInstanceState: Bundle?) {

        orderId = intent.getString("orderId")
        payName = intent.getString("payName")

        binding.titleBar.title = "额外费用收款"
        binding.titleBar.leftImageView().click { finish() }

        binding.tvPaySuccess.clickWithTrigger {
            setResult(200)
            finish()
        }
    }

    override fun initData() {
        vm.makeCostScanPayOrder(orderId)
    }

    override fun startObserve() {
        vm.makeCostScanPayOrderData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                binding.tvPrice.text = "￥${it?.price}"
                binding.ivImage.load(it?.pay_code_url)
            }
        }
    }

}